package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.annotation.LoginUser;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/userinfo")
public class UserInfoController {
    @Reference
    private IUserInfoService userInfoService;

    @Value("${file.dir}")
    private String dir;
    @RequestMapping("/userinfo")
    public String setting(Model model,Long id){
        //查询用户信息
        UserInfo user = userInfoService.getById(id);
        //修改手机格式
        String phone = user.getPhone();
        user.setPhone(phone.substring(0, 3) + "****" + phone.substring(7, phone.length()));
        model.addAttribute("user", user);

        return "/homepage/setting";
    }


    @RequestMapping("/update")
    public String update(UserInfo user){
        userInfoService.update(user);
        return "/userinfo/userinfo";
    }
    //获取资料完整度
    @RequestMapping("/integrity")
    @ResponseBody
    public Integer integrity(Long id){
        Integer integrity = userInfoService.getIntegrity(id);
        return integrity;
    }

    //更换头像
    @RequestMapping("/headImageUpload")
    @ResponseBody
    public Object headImageUpload(MultipartFile pic,@LoginUser UserInfo userInfo){
        if(userInfo!=null){
            String upload = UploadUtil.upload(pic, dir);
            //更新头像到数据库
            userInfoService.updateHeadImg(userInfo.getId(),upload);
            return upload;
        }
        return null;
    }
    //验证手机
    @RequestMapping("/verifyPhone")
    @ResponseBody
    public AjaxResult verifyPhone(Long id, String phone){

        return userInfoService.verifyPhone(id,phone);
    }
    //验证邮箱
    @RequestMapping("/verifyEmail")
    @ResponseBody
    public AjaxResult verifyEmail(Long id, String email){

        return userInfoService.verifyEmail(id,email);
    }
    //验证邮箱
    @RequestMapping("/verifyPEP")
    @ResponseBody
    public AjaxResult verifyPEP(HttpSession session, Long id, String phone, String email, String password){
        AjaxResult result = userInfoService.verifyPEP(id, phone, email, password);
        if(result.isSuccess()){
            session.removeAttribute(Consts.USER_INFO);
        }
        return result;
    }
    //修改密码
    @RequestMapping("/changePW")
    @ResponseBody
    public AjaxResult changePW(Long id, String password){

        return userInfoService.changePW(id,password);
    }
    //修改手机
    @RequestMapping("/changePhone")
    @ResponseBody
    public AjaxResult changePhone(Long id, String phone){

        return userInfoService.changePhone(id,phone);
    }
    //绑定邮箱
    @RequestMapping("/bindingEmail")
    @ResponseBody
    public AjaxResult bindingEmail(Long id, String email){

        return userInfoService.bindingEmail(id,email);
    }
}
